require "scripts.少年光头.Helper.deepcopy"
require "scripts.少年光头.Helper.randomAttr"
diyitem = require "scripts.少年光头.Helper.classic":extend()
---获取经验方式类型
diyitem.ExWhyKey = {
    kill = "杀敌",
}
function diyitem:new()
    self.item = {}
    self.itemSuit = {}
    ---添加套装随机属性
    for key, value in pairs(IteamSuitsAttr) do
        IteamSuitsAttr[key][randomAttr:attr()] = randomAttr:value()
        ---print_mbr(IteamSuitsAttr[key])
    end
    return self
end

-- {
--         Name = "地狱光头不光diy剑-成长剑",
--         ---定义为diy武器
--         _type = "diy",
--         ---需要升级经验
--         upex=1000,
--         ---武器等级
--         lv=1,

--         ---成长属性
--         growing={
--             attack_speed="+10",
--             hemophagia="+100"
--         }
--     }

---添加itemData
function diyitem:add(key)
    local itemTable = deep_copy(hslk.n2v(hitem.getName(key)))
    local data = deep_copy(itemTable)
    
    ---属性
    data._attr = {}
    ---附魔属性
    data.enchant = data.enchant or nil
    ---套装属性
    ---data.osuit = data.suit or nil
    -- ---套装单属性
    -- data.onesuit = data.onesuit or nil
    -- ---套装名字
    -- data.suitName = data.suitName or nil
    ---成长属性
    data.growing = data.growing or nil
    ---升级所需经验
    data.upex = data.upex or nil
    ---获得经验
    data.getex = data.getex or nil
    ---等级
    data.lv = data.lv or nil
    ---经验获取方式
    data.ExWhy = data.ExWhy or nil
    self.item[key] = {
        ---diy武器数据
        itemData = data,
        ---绑定单位
        holderUnit = nil,
        ---杀敌事件
        onKillid = nil
        ---丢弃事件
        -- ItemDropId = nil
    }

    self.UnitInfo = {}
    self:ItemObtained(key)
    return self
end
---修改技能
---@param fun onItemUsed | "function(evtData) end"
function diyitem:Skill(key, fun)
    IteamUsed:Skill(key, fun)
    ---print_mbr(IteamUsed.Used)
end
---将成长属性添加到属性上
function diyitem:growing(key)
    --计算新属性table
    local new_attr = deep_copy(self.item[key].itemData._attr)
    for k, value in pairs(self.item[key].itemData.growing) do
        local attrv = self.item[key].itemData._attr[k]

        if new_attr[k] == nil then
            new_attr[k] = tostring(value)
        else
            new_attr[k] = "+" .. tostring(tonumber(tostring(string.gsub(attrv, "=", "", 1))) + tonumber(value))
        end
    end
    self:updateAttr(key, new_attr, nil)
    --print_mbr("添加成长属性")
    return self
end

---随机增加基础属性
function diyitem:randomAttr(key)
    local tempAttr = deep_copy(self.item[key].itemData._attr)
    tempAttr[randomAttr:attr()] = randomAttr:value()

    self:updateAttr(key, tempAttr, nil)

    return self
end

---随机增加附魔属性
function diyitem:randomEnchant(key)
    local tempAttr = deep_copy(self.item[key].itemData.enchant)
    tempAttr[randomAttr:attr()] = randomAttr:value()

    self:updateAttr(key, nil, tempAttr)
    return self
end

---删除武器
function diyitem:del(key)
    if self.item[key] ~= nil then
        self:RemoveDiyAttributes(key)
        self.item[key] = nil
    end

    return self
end

---设置武器数量
function diyitem:SetItemCharges(key, number)
    self:RemoveDiyAttributes(key)
    hitem.setCharges(key, number)
    self:AddAttributes(key)
    return self
end
---计算单位经验值
function diyitem:exCalculate(targetUnit, triggerUnit, key)
    local f = 45
    ---死亡单位等级
    local targetUnitLv = cj.GetUnitLevel(targetUnit)
    ---凶手单位等级
    local triggerUnitlv = cj.GetUnitLevel(triggerUnit)
    ---武器等级
    local lv = self.item[key].itemData.lv
    -- print_mbr("死亡单位等级："..triggerUnitlv)
    -- print_mbr("武器等级："..lv)
    if lv > targetUnitLv then
        return f + 1 * lv
    end
    if lv == targetUnitLv then
        return f + 3 * lv
    end
    if lv < targetUnitLv then
        return f + 5 * lv
    end
end
---注册杀敌增加经验
function diyitem:registerGetex(key)
    if self.item[key].itemData.ExWhy ~= nil then
        if self.item[key].itemData.ExWhy == diyitem.ExWhyKey.kill then
            self.item[key].onKillid =
                hevent.onKill(
                self.item[key].holderUnit,
                function(evtData)
                    ---print_mbr(hslk.n2v(hunit.getName(evtData.targetUnit)))
                    ---经验
                    local ex = self:exCalculate(evtData.targetUnit, evtData.triggerUnit, key)
                    print_mbr("经验：" .. ex)
                    self:addGetex(key, ex)
                end
            )
        end
    end

    return self
end


---增加经验
function diyitem:addGetex(key, num)
    self.item[key].itemData.getex = tonumber(self.item[key].itemData.getex) + tonumber(num)
    ---超出部分
    local chaochu = self.item[key].itemData.getex - self.item[key].itemData.upex
    if chaochu >= 0 then
        --self.item[key].itemData.getex = chaochu
        self:lvup(key)
        if chaochu > 0 then
            self:addGetex(key, chaochu)
        end
    end

    return self
end
---升级
function diyitem:lvup(key)
    ---当前等级
    local Level = tonumber(self.item[key].itemData.lv)
    ---当前升级所需
    local upex = tonumber(self.item[key].itemData.upex)
    ---初始值
    local chushi = upex / Level

    ---升级后
    self.item[key].itemData.lv = self.item[key].itemData.lv + 1

    self.item[key].itemData.upex = chushi * self.item[key].itemData.lv
    ---获得经验=0
    self.item[key].itemData.getex = 0
    echo(
        self.item[key].itemData.slk.Name .. "升级lv" .. self.item[key].itemData.lv,
        hunit.getOwner(self.item[key].holderUnit)
    )
    self:growing(key)
    ---print_mbr("升级前" .. Level .. "," .. "经验" .. upex .. "初始值" .. chushi)
    return self
end

---更新itemData
function diyitem:update(key, value)
    self.item[key].itemData = value
    return self
end
---更新属性加成 更新附加属性
function diyitem:updateAttr(key, new_attr, new_enchant)
    --去除旧属性
    self:RemoveDiyAttributes(key)
    --更新属性
    if new_attr ~= nil then
        self.item[key].itemData._attr = new_attr
    end
    --更新附加属性
    if new_enchant ~= nil then
        self.item[key].itemData.enchant = new_enchant
    end

    --附加新属性
    self:AddAttributes(key)
    return self
end
---获取itemData
function diyitem:get(key)
    if self.item[key] ~= nil then
        return self.item[key].itemData
    end
    return nil
end
---物品当前持有单位
function diyitem:ItemObtained(key)
    htime.setInterval(
        0.03,
        function(t)
            local holderUnit = hitem.getHolder(key)

            if holderUnit ~= nil then
                self.item[key].holderUnit = holderUnit
                if self.UnitInfo[self.item[key].holderUnit] == nil then
                    self.UnitInfo[self.item[key].holderUnit] = {
                        ---套装是否以加成
                        isSuit = false,
                        ---丢弃事件
                        ItemDropId = nil
                    }
                end

                --self:RemoveAttributes(key)
                self:ItemDrop(key)
                self:AddAttributes(key)
                self:registerGetex(key)
                ---self:Suit(key)
                t.destroy()
            end
        end
    )
    return self
end
---检测套装
function diyitem:Suit(key)
    local count = 0

    if self.item[key] ~= nil and self.item[key].holderUnit ~= nil then
        hitem.forEach(
            self.item[key].holderUnit,
            function(enumItem, slotIndex)
                if enumItem == nil then
                    return
                end
                local taoName = IteamUsed:getSuitKey(enumItem)
                if IteamSuits[taoName] ~= nil then
                    for index, value in ipairs(IteamSuits[taoName]) do
                        if value == hitem.getName(enumItem) then
                            --print_mbr(value)
                            count = count + 1
                        end
                    end
                end
            end
        )
    end

    ---print_mbr(count)
    return count
end

---当单位丢弃装备
function diyitem:ItemDrop(key)
    if self.UnitInfo[self.item[key].holderUnit].ItemDropId == nil then
        -- echo("删除")
        -- hevent.deleteEvent(self.item[key].holderUnit, CONST_EVENT.itemDrop, self.item[key].ItemDropId)
        -- self.item[key].ItemDropId = nil
        self.UnitInfo[self.item[key].holderUnit].ItemDropId =
            hevent.onItemDrop(
            self.item[key].holderUnit,
            function(evtData)
                key = evtData.triggerItem
                if self.item[key] == nil then
                    return
                end
                self:RemoveDiyAttributes(key)
                local holderUnit = self.item[key].holderUnit
                self.item[key].holderUnit = nil
                -- local ItemDropId= self.item[key].ItemDropId
                -- self.item[key].ItemDropId = nil
                self:ItemObtained(key)

                if self.item[key].onKillid ~= nil then
                    hevent.deleteEvent(holderUnit, CONST_EVENT.kill, self.item[key].onKillid)
                    self.item[key].onKillid = nil
                end
            end
        )
    end
end

---削减单位获得物品后的属性
function diyitem:subProperty(whichUnit, itId, charges, attr)
    if (whichUnit == nil or itId == nil or charges < 1 or attr == nil) then
        return
    end
    hattribute.set(whichUnit, 0, {weight_current = "-" .. hitem.getWeight(itId, charges)})
    hattribute.caleAttribute(CONST_DAMAGE_SRC.item, false, whichUnit, attr, charges)
    for _ = 1, charges, 1 do
        hring.remove(whichUnit, itId)
    end
end

--- 附加单位获得物品后的属性
function diyitem:addProperty(whichUnit, itId, charges, attr)
    if (whichUnit == nil or itId == nil or charges < 1 or attr == nil) then
        return
    end
    hattribute.set(whichUnit, 0, {weight_current = "+" .. hitem.getWeight(itId, charges)})
    hattribute.caleAttribute(CONST_DAMAGE_SRC.item, true, whichUnit, attr, charges)
    for _ = 1, charges, 1 do
        hring.insert(whichUnit, itId)
    end
end

---去除diy属性加成
---@param key number
---@param number any
function diyitem:RemoveDiyAttributes(key)
    --获得物品数量
    local charges = hitem.getCharges(key)
    --print_mbr(charges)
    --消减属性
    self:subProperty(self.item[key].holderUnit, key, charges, self.item[key].itemData._attr)
    self:subProperty(self.item[key].holderUnit, key, charges, self.item[key].itemData.enchant)
    self:subProperty(self.item[key].holderUnit, key, charges, self.item[key].itemData.onesuit)

    if self.UnitInfo[self.item[key].holderUnit] ~= nil then
        if
            (self:Suit(key) < 6 and self.UnitInfo[self.item[key].holderUnit].isSuit == true) or
                self.UnitInfo[self.item[key].holderUnit].isSuit == true
         then
            ---套装名字
            local TaoName = IteamUsed:getSuitKey(key)
            self:subProperty(self.item[key].holderUnit, key, charges, IteamSuitsAttr[TaoName])
            echo("套装加成去除")
            self.UnitInfo[self.item[key].holderUnit].isSuit = false
        end
    end

    ---echo("消除diy物品加成")
end

---附加diy物品加成
function diyitem:AddAttributes(key)
    --获得物品数量
    local charges = hitem.getCharges(key)
    --print_mbr(charges)
    --附加属性
    self:addProperty(self.item[key].holderUnit, key, charges, self.item[key].itemData._attr)
    self:addProperty(self.item[key].holderUnit, key, charges, self.item[key].itemData.enchant)
    self:addProperty(self.item[key].holderUnit, key, charges, self.item[key].itemData.onesuit)
    if self:Suit(key) == 6 and self.UnitInfo[self.item[key].holderUnit].isSuit == false then
        local TaoName = IteamUsed:getSuitKey(key)
        self:addProperty(self.item[key].holderUnit, key, charges, IteamSuitsAttr[TaoName])
        echo("套装加成附加")
        self.UnitInfo[self.item[key].holderUnit].isSuit = true
    end
    ---echo("附加diy物品加成")
end
